package com.kitbox.hl.meshow.bpm.service.impl;


import com.goshine.sgcloud.platform.common.spring.PropertiesUtil;
import com.kitbox.hl.meshow.bpm.bean.BPMWorkItem;
import com.kitbox.hl.meshow.bpm.bean.BpmNodeVo;
import com.kitbox.hl.meshow.bpm.bean.WFActivityInst;
import com.kitbox.hl.meshow.bpm.service.IBpmService;
import com.kitbox.hl.meshow.bpm.utils.ClientUtils;
import com.kitbox.hl.meshow.bpm.utils.EntityMappingUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.Property;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BpmServiceImpl implements IBpmService {




    public static void main(String[] args) throws Exception {
        new BpmServiceImpl().createProcessInstance("tiger","leave","20180521Loan10","请假流程》=3天",true);

    	//new BpmServiceImpl().finishWorkItemWithOption("2c9082e861f514040162222252a2007f", 3039, "isAgree=Y", false);
    	
    	
//    	List<BPMWorkItem> list=new BpmServiceImpl().queryBacklogAndFinishWorkItemsByProcessInstId(4028+"", false);
//    	if(list!=null){
//
//    		System.out.println(list.get(0).getWorkItemID());
//    	}
    	
    }

    @Override
    public String createProcessInstance(String userID, String processDefName, String processInstName, String processInstDesc, Boolean isStart) throws Exception {
        String url= PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="createProcessInstance";
        Object[] obj=new Object[]{userID,processDefName,processInstName,processInstDesc,isStart};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        String flowId = ClientUtils.xmlElementsReturnString(resultXml);
        //System.out.println("创建流程实例成功——》流程实例ID:"+flowId);
        return StringUtils.isNotBlank(flowId)?(flowId):null;
    }

    @Override
    public List<BPMWorkItem> queryPersonWorkItems(String userID, String personID, String permission, String scope) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="queryPersonWorkItems";
        Object[] obj=new Object[]{userID,personID,permission,scope};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        List<Map<String,String>> list = ClientUtils.xmlElementsReturnList(resultXml);
        List<BPMWorkItem> wList= new ArrayList<>();
        for(Map<String,String> map:list) {
            BPMWorkItem workItem=new BPMWorkItem();
            EntityMappingUtil.copyProperites(workItem,map);
            wList.add(workItem);
        }
        return wList;
    }

    @Override
    public String appointActivityParticipant(String userId, long workItemID, String activityDefID, String participantsxml) throws Exception {
        return null;
    }

    @Override
    public String assignWorkItemToSelf(String userID, long workItemid) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="assignWorkItemToSelf";
        Object[] obj=new Object[]{userID,workItemid};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        String resultStr = ClientUtils.xmlElementsReturnString(resultXml);
        return resultStr;
    }

    @Override
    public String assignAndFinishWorkItemWithOption(String userID, long workItemid, String relativeData, boolean checkPermission) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="assignAndFinishWorkItemWithOption";
        Object[] obj=new Object[]{userID,workItemid,relativeData,checkPermission};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        String resultStr = ClientUtils.xmlElementsReturnString(resultXml);
        return resultStr;
    }

    @Override
    public List<BPMWorkItem> queryPersonFinishedWorkItems(String userID, String personID, String permission, Boolean includeHistory) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="queryPersonFinishedWorkItems";
        Object[] obj=new Object[]{userID,personID,permission,includeHistory};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        List<Map<String,String>> list = ClientUtils.xmlElementsReturnList(resultXml);
        List<BPMWorkItem> wList= new ArrayList<>();
        for(Map<String,String> map:list) {
            BPMWorkItem workItem=new BPMWorkItem();
            EntityMappingUtil.copyProperites(workItem,map);
            wList.add(workItem);
        }
        return wList;
    }

    @Override
    public List<BPMWorkItem> queryWorkItemsAndReviewByProcessInstId(long ProcessInstId, Boolean includeHistory) throws IOException {
        String url=PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="queryWorkItemsAndReviewByProcessInstId";
        Object[] obj=new Object[]{ProcessInstId,includeHistory};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        List<Map<String,String>> list = ClientUtils.xmlElementsReturnList(resultXml);
        List<BPMWorkItem> wList= new ArrayList<>();
        for(Map<String,String> map:list) {
            BPMWorkItem workItem=new BPMWorkItem();
            EntityMappingUtil.copyProperites(workItem,map);
            wList.add(workItem);
        }
        return wList;
    }

    @Override
    public List<BPMWorkItem> queryPersonAllWorkItems(String userID, String personID, String permission, String scope, Boolean includeHistory, long processInstID) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="queryPersonAllWorkItems";
        Object[] obj=new Object[]{userID,personID,permission,scope,includeHistory,processInstID};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        List<Map<String,String>> list = ClientUtils.xmlElementsReturnList(resultXml);
        List<BPMWorkItem> wList= new ArrayList<>();
        for(Map<String,String> map:list) {
            BPMWorkItem workItem=new BPMWorkItem();
            EntityMappingUtil.copyProperites(workItem,map);
            wList.add(workItem);
        }
        return wList;
    }

    @Override
    public List<BPMWorkItem> queryBacklogAndFinishWorkItemsByProcessInstId(String ProcessInstId, boolean includeHistory) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="queryBacklogAndFinishWorkItemsByProcessInstId";
        Object[] obj=new Object[]{ProcessInstId,includeHistory};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        List<Map<String,String>> list = ClientUtils.xmlElementsReturnList(resultXml);
        List<BPMWorkItem> wList= new ArrayList<>();
        for(Map<String,String> map:list) {
            BPMWorkItem workItem=new BPMWorkItem();
            EntityMappingUtil.copyProperites(workItem,map);
            wList.add(workItem);
        }
        return wList;
    }

    @Override
    public void finishActivityInstance(String userID, long activityInstID) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.WSActivityInstManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSActivityInstManagerService");
        String method="finishActivityInstance";
        Object[] obj=new Object[]{userID,activityInstID};
        ClientUtils.client(url,nameSpace,method,obj);
        //System.out.println("处理成功");
    }

    @Override
    public String finishWorkItemWithOption(String userID, long workItemid, String relativeData, boolean checkPermission) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="finishWorkItemWithOption";
        Object[] obj=new Object[]{userID,workItemid,relativeData,checkPermission};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        String resultStr = ClientUtils.xmlElementsReturnString(resultXml);
        return resultStr;
    }

    @Override
    public String batchReassignWorkItem(String userID, String jsonStr) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.WSBatchInterfaceManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSBatchInterfaceManagerService");
        String method="batchReassignWorkItem";
        Object[] obj=new Object[]{userID,jsonStr};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        String resultStr = ClientUtils.xmlElementsReturnString(resultXml);
        //System.out.println(resultStr);
        return resultStr;
    }

    @Override
    public WFActivityInst queryPreviousActivityInstances(String userID, long activityInstID) throws Exception {
       // String url="http://172.16.2.87:8099/default/services/WSActivityInstManagerService?wsdl";
        //String nameSpace="http://www.primeton.com/services/WSActivityInstManagerService";

        String url=PropertiesUtil.getProperty("bpm.wsdl.WSActivityInstManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSActivityInstManagerService");

        String method="queryPreviousActivityInstances";
        Object[] obj=new Object[]{userID,activityInstID};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        System.out.println(resultXml);
        List<Map<String,String>> list =  ClientUtils.xmlElementsReturnList(resultXml);
        WFActivityInst activityInst=new WFActivityInst();
        for(Map<String,String> map:list) {
            EntityMappingUtil.copyProperites(activityInst,map);
        }
        return activityInst;
    }

    @Override
    public void backActivity(String userId, long currentActInstID, long destActInstID, String rollBackStrategy) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.WSBackActivityManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSBackActivityManagerService");
        String method="backActivity";
        Object[] obj=new Object[]{userId,currentActInstID,destActInstID,rollBackStrategy};
        ClientUtils.client(url,nameSpace,method,obj);
        //System.out.println("回退成功");
    }

    @Override
    public void backDefActivity(String userId, long currentActInstID, String destActDefID, String rollBackStrategy) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.WSBackActivityManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSBackActivityManagerService");
        String method="backDefActivity";
        Object[] obj=new Object[]{userId,currentActInstID,destActDefID,rollBackStrategy};
        ClientUtils.client(url,nameSpace,method,obj);
    }

    @Override
    public String changeProcessInstState(String userID, long processInstID, int destState) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="changeProcessInstState";
        Object[] obj=new Object[]{userID,processInstID,destState};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        String resultStr = ClientUtils.xmlElementsReturnString(resultXml);
        //System.out.println(resultStr);
        return resultStr;
    }

    @Override
    public String terminateProcessInstance(String userID, long processInstID) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="terminateProcessInstance";
        Object[] obj=new Object[]{userID,processInstID};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        String resultStr = ClientUtils.xmlElementsReturnString(resultXml);
        //System.out.println(resultStr);
        return resultStr;
    }

    @Override
    public String getActivityInstState(String userID, long activityInstID) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.WSActivityInstManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSActivityInstManagerService");
        String method="getActivityInstState";
        Object[] obj=new Object[]{userID,activityInstID};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        String resultStr = ClientUtils.xmlElementsReturnString(resultXml);
        //System.out.println(resultStr);
        return resultStr;
    }

    @Override
    public String removeActivityInstance(String userID, long actInstID) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.WSActivityInstManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSActivityInstManagerService");
        String method="removeActivityInstance";
        Object[] obj=new Object[]{userID,actInstID};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        String resultStr = ClientUtils.xmlElementsReturnString(resultXml);
        //System.out.println(resultStr);
        return resultStr;
    }

    @Override
    public void restartActivityInstance(String userID, long activityInstID) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.WSActivityInstManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSActivityInstManagerService");
        String method="restartActivityInstance";
        Object[] obj=new Object[]{userID,activityInstID};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        //System.out.println("");
    }

    @Override
    public void resumeActivityInstance(String userID, long activityInstID) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.WSActivityInstManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSActivityInstManagerService");
        String method="resumeActivityInstance";
        Object[] obj=new Object[]{userID,activityInstID};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        //System.out.println("");
    }

    @Override
    public void suspendActivityInstance(String userID, long activityInstID) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.WSActivityInstManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSActivityInstManagerService");
        String method="suspendActivityInstance";
        Object[] obj=new Object[]{userID,activityInstID};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        //System.out.println("");
    }

    @Override
    public void terminateActivityInstance(String userID, long activityInstID) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.WSActivityInstManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSActivityInstManagerService");
        String method="terminateActivityInstance";
        Object[] obj=new Object[]{userID,activityInstID};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        //System.out.println("");
    }

    @Override
    public List<BpmNodeVo> getProcess(String userID, long processDefID) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.WSDefinitionQueryManagerService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.WSDefinitionQueryManagerService");
        String method="getProcess";
        Object[] obj=new Object[]{userID,processDefID};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        return ClientUtils.XmlExam(resultXml);
    }

    /**
     * 通过流程实例 ID  流程实例状态
     *
     * @param userID
     * @param processInstID
     * @return
     */
    @Override
    public String getProcessInstState(String userID, long processInstID) throws Exception {
        String url=PropertiesUtil.getProperty("bpm.wsdl.SimpleParameterProcessLifecycleService");
        String nameSpace=PropertiesUtil.getProperty("bpm.ns.SimpleParameterProcessLifecycleService");
        String method="getProcessInstState";
        Object[] obj=new Object[]{userID,processInstID};
        String resultXml = ClientUtils.client(url,nameSpace,method,obj);
        String status = ClientUtils.xmlElementsReturnString(resultXml);
        System.out.println("创建流程实例状态——》流程实例ID:"+status);
        return StringUtils.isNotBlank(status)?status:null;
    }
}
